package fitnesshub.controller;

import fitnesshub.pojo.dto.AddCourseDTO;
import fitnesshub.pojo.dto.CoursePageQueryDTO;
import fitnesshub.pojo.entity.Course;
import fitnesshub.result.PageResult;
import fitnesshub.result.Result;
import fitnesshub.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 课程管理
 */
@RestController
@RequestMapping("/admin/course")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 课程分页
     *
     * @param coursePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(CoursePageQueryDTO coursePageQueryDTO) {
        log.info("课程分页查询，参数：{}", coursePageQueryDTO);
        PageResult pageResult = courseService.page(coursePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询课程
     *
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}")
    public Result<Course> getById(@PathVariable Integer courseId) {
        log.info("根据id:{},查询课程", courseId);
        Course course = courseService.getById(courseId);
        return Result.success(course);
    }

    /**
     * 新增课程
     *
     * @param addCourseDTO
     * @return
     */
    @PostMapping
    public Result addACourse(@RequestBody AddCourseDTO addCourseDTO) {
        log.info("新增课程:{}", addCourseDTO);
        courseService.addACoach(addCourseDTO);
        return Result.success();
    }

    /**
     * 编辑课程
     *
     * @param course
     * @return
     */
    @PutMapping
    public Result editCourse(@RequestBody Course course) {
        log.info("编辑课程:{}", course);
        courseService.editCourse(course);
        return Result.success();
    }

    /**
     * 根据id删除课程
     *
     * @param courseId
     * @return
     */
    @DeleteMapping("{courseId}")
    public Result deleteById(@PathVariable Integer courseId) {
        log.info("删除课程,id:{}", courseId);
        courseService.deleteById(courseId);
        return Result.success();
    }
}
